import Vue from 'vue'
import Vuex from 'vuex'
// 导入token方法
import * as auth from '../utils/auth'
import { login } from '../api/user'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: auth.getToken() || {}
  },
  mutations: {
    // 添加token
    setToken (state, token) {
      state.user = token
      auth.setToken(token)
    },
    // 删除token
    delToken (state) {
      state.user = {}
      auth.removeToken()
    }
  },
  actions: {
    async getToken ({ commit }, from) {
      // console.log(form)
      const { data } = await login(from)
      console.log('token:', data)
      commit('setToken', data)
    }
  },
  modules: {
  }
})
